package com.maomao.maomaochong_i;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.maomao.maomaochong_i.adapterPackage.ViewPagerAdaper;
import com.maomao.maomaochong_i.application.MyApplication;
import com.maomao.maomaochong_i.guowuche.ShiJian;
import com.maomao.maomaochong_i.quanxing.CeShi;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Context context;

    private FragmentManager fManager;

    private final TuFragment FG[] = new TuFragment[5];

    private final TextView DITUVIEW[] = new TextView[5];

    private final int DITUVIEWID[] = {0,R.id.dibu1,R.id.dibu2,R.id.dibu3,R.id.dibu4};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ceshi);
        MyApplication.getInstance().addActivity(this);
        CeShi ceShi = new CeShi();
        context = MainActivity.this;
        tongming(R.color.beijing);
        fManager = getFragmentManager();
        getdituviewId();
        if (getValue() == true){
            DITUVIEW[4].performClick();
        }
        else {
            DITUVIEW[1].performClick();
        }
    }

    private void tongming(int yangse) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(yangse));
        }
    }//设置状态栏的颜色


    private void getdituviewId(){
        DiBuShiJian buttonShi = new DiBuShiJian();
        for (int i = 1;i<DITUVIEW.length;i++){
            DITUVIEW[i] = (TextView) findViewById(DITUVIEWID[i]);
            DITUVIEW[i].setOnClickListener(buttonShi);
        }
    }

    private class DiBuShiJian implements View.OnClickListener{

        FragmentTransaction fragmentTransaction;
        public void onClick(View v) {
            fragmentTransaction  = fManager.beginTransaction();

            yingchang(fragmentTransaction);

            int color = R.color.beijing;

            switch (v.getId()){
                case R.id.dibu1:
                    weizhi(YeMian.SHOUYE);
                    color = R.color.beijing;
                    break;

                case R.id.dibu2:
                    weizhi(YeMian.FENGLEI,context,fManager);
                    break;

                case R.id.dibu3:
                    weizhi(YeMian.GOUWUCHE);

                    break;


                case R.id.dibu4:
                    weizhi(YeMian.GEREN,context,fManager);

                    color = R.color.geren;
                    break;
            }

            tongming(color);
        }

        private void weizhi(YeMian ye){
            xianshiyemian(ye.getWeizhi());
        }

        private void weizhi(YeMian ye,Context context, FragmentManager fManager){
            xianshiyemian(ye.getWeizhi(),context,fManager);
        }
        private void xianshiyemian(int number){
            if(FG[number] == null){
                FG[number] = new TuFragment(number);
                fragmentTransaction.add(R.id.ShouYeFrame,FG[number]);
            }else {
                if(number == 3) {
                    ShiJian.getZhuanti();
                }
                FG[number].getMaoView().invalidate();
                fragmentTransaction.show(FG[number]);
            }
            fragmentTransaction.commit();
        }

        private void xianshiyemian(int number,Context context, FragmentManager fManager){
            if(FG[number] == null){
                FG[number] = new TuFragment(context,fManager,number);
                fragmentTransaction.add(R.id.ShouYeFrame,FG[number]);
            }else {
                fragmentTransaction.show(FG[number]);
            }
            fragmentTransaction.commit();
        }

    }

    private void yingchang(FragmentTransaction fragmentTransaction){
        for(int i = 1;i<FG.length;i++){
            if(FG[i] != null)
                fragmentTransaction.hide(FG[i]);
        }
    }

    //在主Activity中按手机返回键
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_BACK){
            moveTaskToBack(false);
            //关闭所有activity，并退出程序
            MyApplication.getInstance().exit();
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    private Boolean getValue(){
        Boolean value = null;
        Intent intent = getIntent();
        if (intent != null){
            try{
                value = intent.getBooleanExtra("boolean",false);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return value;
    }
}
